package com.blog.page;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * 分页标签基本类
 */
@SuppressWarnings("serial")
public class PageTag extends BodyTagSupport {
    private static final Log LOG = LogFactory.getLog(PageTag.class);

	private String value = null;
    //		限制每屏显示的页码个数 ，如：上一页  1 2 3 4 5 下一页该值表示中间显示多少个页码
    private int pageNum = 5;
    //    分页属性
    private Object commonList;
    //    分页策略
    private PagesModel3 pagesModel;

    private String uri;
    private String search;
    private String target; // 装载或替换的html容器
    private Integer replace = 0; // 默认不覆盖，即内容替换
    @Override
    public int doStartTag() {
        if (null == commonList || !(commonList instanceof CommonList)) {
            return EVAL_PAGE;
        }
        CommonList cl = (CommonList) commonList;

        if (null != uri)
            cl.setUri(uri);
        if (null != target)
            cl.setTarget(target);

        if(null != search){
        	cl.setSearchStr(search);
        }
        cl.setReplace(replace);
        
        pagesModel = new PagesModel3();      
        String pagestr = pagesModel.genPageHtml(cl, pageNum);
        try {
            JspWriter writer = pageContext.getOut();
            writer.write(pagestr);
        } catch (IOException e) {
            LOG.error("page tag is error:",e);
        }
        return EVAL_PAGE;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Object getCommonList() {
        return commonList;
    }


    public void setCommonList(Object commonList) {
        this.commonList = commonList;
    }


    public String getUri() {
        return uri;
    }


    public void setUri(String uri) {
        this.uri = uri;
    }


    public String getTarget() {
        return target;
    }


    public void setTarget(String target) {
        this.target = target;
    }


    public Integer getReplace() {
        return replace;
    }


    public void setReplace(Integer replace) {
        this.replace = replace;
    }

    public String getSearch() {
		return search;
	}


	public void setSearch(String search) {
		this.search = search;
	}
}

